package com.you.weixinpay.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.you.weixinpay.domain.entity.TOrderInfo;
import com.you.weixinpay.domain.entity.TProduct;
import com.you.weixinpay.enums.OrderStatus;
import com.you.weixinpay.mapper.TOrderInfoMapper;
import com.you.weixinpay.mapper.TProductMapper;
import com.you.weixinpay.service.TOrderInfoService;
import com.you.weixinpay.utils.OrderNoUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yzm
 * @since 2025-06-15
 */
@Service
public class TOrderInfoServiceImpl extends ServiceImpl<TOrderInfoMapper, TOrderInfo> implements TOrderInfoService {

    @Autowired
    private TProductMapper productMapper;
    @Override
    public TOrderInfo createOrderByProductId(Long productId,String paymentType, Long userId) {

        //查找已存在但未支付的订单，并且订单类型是微信支付
        LambdaQueryWrapper<TOrderInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TOrderInfo::getProductId, productId)
                .eq(TOrderInfo::getOrderStatus, OrderStatus.NOTPAY.getType())
                .eq(TOrderInfo::getUserId, userId)
                .eq(TOrderInfo::getPaymentType, paymentType);
        TOrderInfo orderInfo = this.getOne(queryWrapper);
        if( orderInfo != null){
            return orderInfo;
        }

        //获取商品信息
        TProduct product = productMapper.selectById(productId);

        //生成订单
        orderInfo = new TOrderInfo();
        orderInfo.setTitle(product.getTitle());
        orderInfo.setOrderNo(OrderNoUtils.getType(paymentType)+OrderNoUtils.getOrderNo()); //订单号
        orderInfo.setProductId(productId);
        orderInfo.setTotalFee(product.getPrice()); //分
        orderInfo.setOrderStatus(OrderStatus.NOTPAY.getType());
        orderInfo.setPaymentType(paymentType);
        orderInfo.setUserId(userId);
        baseMapper.insert(orderInfo);

        return orderInfo;
    }

    @Override
    public void updateStatusByOrderNo(String outTradeNo, String status) {
        LambdaQueryWrapper<TOrderInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TOrderInfo::getOrderNo, outTradeNo);
        TOrderInfo orderInfo = this.getOne(queryWrapper);
        orderInfo.setOrderStatus(status);
        baseMapper.update(orderInfo, queryWrapper);
    }

    @Override
    public String getOrderStatus(String outTradeNo) {
        LambdaQueryWrapper<TOrderInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TOrderInfo::getOrderNo, outTradeNo);
        TOrderInfo orderInfo = this.getOne(queryWrapper);
        if(orderInfo == null){ //防止空指针异常
            return null;
        }
        return orderInfo.getOrderStatus();
    }

    @Override
    public List<TOrderInfo> getUnpaidOrders(LocalDateTime createTimeBefore) {
        return this.list(new LambdaQueryWrapper<TOrderInfo>()
                .eq(TOrderInfo::getOrderStatus, OrderStatus.NOTPAY.getType())
                .le(TOrderInfo::getCreateTime, createTimeBefore));
    }

    @Override
    public Page<TOrderInfo> getList(Page<TOrderInfo> page, String title) {
        return this.page(page, new LambdaQueryWrapper<TOrderInfo>()
                .like(TOrderInfo::getTitle, title)
                .orderByDesc(TOrderInfo::getCreateTime));
    }

    @Override
    public TOrderInfo getOrderByOrderNo(String outTradeNo) {
        return this.getOne(new LambdaQueryWrapper<TOrderInfo>()
                .eq(TOrderInfo::getOrderNo, outTradeNo));
    }
}
